﻿/*******************************************************
*类名称：ReplyMsgManager
*版本号：V1.0.0.0
*作者：成俊杰
*CLR版本：4.0.30319.36264
*创建时间：2017-08-18 15:09:52
*说明：T_ReplyMsg管理类
*更新备注：
**********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wechat.Dao;
using Wechat.Dao.Implement;
using Wechat.Domain;
using Wechat.Dto.Request;
using Wechat.Dto.Search;
using Wechat.Util.ReturnMessage;
using Wechat.Util.StateCode;
using Wechat.Dto.Extension;

namespace Wechat.Manager.Implement
{
    /// <summary>
    /// T_ReplyMsg
    /// </summary>
    public class ReplyMsgManager : GenericManagerBase<ReplyMsg>, IReplyMsgManager
    {
        public IReplyMsgRepository ReplyMsgRepository { get; set; }
        /// <summary>
        /// 添加微信回复消息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResultMessage ReplyMsgInsert(ReplyMsgRequest request)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = "插入数据失败"
            };
            if (this.CurrentRepository is IReplyMsgRepository)
            {
                string condition = @"SELECT COUNT(1) FROM T_ReplyMsg WHERE ReciveMsg=@ReciveMsg 
                   AND  IsDeleted<>@IsDeleted  ";//查询除当前菜单外，是否有重名且未删除的菜单
                var ReplyMsg = Convert.ToInt32((this.CurrentRepository as IReplyMsgRepository).ExecuteScalar(condition, new { ReciveMsg = request.ReciveMsg, IsDeleted = 1 }));
                if (ReplyMsg == 0)
                {
                    var model = request.ToModel();
                    model.IsEnable = true;
                    var result = (this.CurrentRepository as IReplyMsgRepository).Insert(model);
                    if (result > 0)
                    {
                        returnMsg.Code = (int)EnumApiStateCode.Success;
                        returnMsg.Message = "插入数据成功";
                        returnMsg.Status = true;
                        returnMsg.Data = result;
                    }
                }
                else
                {
                    returnMsg.Code = (int)EnumApiStateCode.ParameterValidationFailed;
                    returnMsg.Message = "微信回复消息名已存在";
                    returnMsg.Status = false;
                }
            }
            return returnMsg;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="idArray"></param>
        /// <returns></returns>
        public ResultMessage ReplyMsgDeleteById(string ids)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = "参数校验失败"
            };
            if (ids.Length > 0)
            {
                string condition = " UPDATE T_ReplyMsg SET IsDeleted=1 WHERE Id IN (" + ids + ")";
                var result = ExecuteSql(condition, null);
                if (result > 0)
                {
                    returnMsg.Code = (int)EnumApiStateCode.Success;
                    returnMsg.Message = "删除数据成功";
                    returnMsg.Status = true;
                    returnMsg.Data = result;
                }
                else
                {
                    returnMsg.Message = "删除数据失败";
                }
            }

            return returnMsg;
        }

        /// <summary>
        /// 修改微信回复消息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResultMessage ReplyMsgUpdateById(ReplyMsgRequest request)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = "修改数据失败"
            };
            if (this.CurrentRepository is IReplyMsgRepository)
            {
                string condition = @"SELECT COUNT(1) FROM T_ReplyMsg WHERE ReciveMsg=@ReciveMsg AND Id<>@Id
                   AND  IsDeleted<>@IsDeleted  ";//查询除当前菜单外，是否有重名且未删除的菜单
                var ReplyMsg = Convert.ToInt32((this.CurrentRepository as IReplyMsgRepository).ExecuteScalar(condition, new { ReciveMsg = request.ReciveMsg, Id = request.Id, IsDeleted = 1 }));

                if (!(ReplyMsg > 0))
                {

                    var model = this.CurrentRepository.Get(request.Id ?? 0);
                    if (model != null)
                    {
                        model.ReciveMsg = request.ReciveMsg;
                        model.ReturnMsg = request.ReturnMsg;
                        model.IsEnable = request.IsEnable ?? model.IsEnable;
                        model.LastUpdateTime = DateTime.Now;
                        var result = (this.CurrentRepository as IReplyMsgRepository).Update(model);
                        if (result)
                        {
                            returnMsg.Code = (int)EnumApiStateCode.Success;
                            returnMsg.Message = "修改数据成功";
                            returnMsg.Status = true;
                            returnMsg.Data = result;
                        }
                    }
                }
                else
                {
                    returnMsg.Code = (int)EnumApiStateCode.ParameterValidationFailed;
                    returnMsg.Message = "微信回复消息名已存在";
                    returnMsg.Status = false;
                }
            }
            return returnMsg;
        }

        /// <summary>
        /// 获取微信回复消息分页信息
        /// </summary>
        /// <returns></returns>
        public ResultMessage GetReplyMsgPageData(ReplyMsgSearch request)
        {
            ResultMessage resultMsg = new ResultMessage()
            {
                Status = false,
                Message = "查询失败",
                Code = (int)EnumApiStateCode.SystemError,
            };
            var pageData = ReplyMsgRepository.GetReplyMsgPageData(request);
            if (pageData != null)
            {
                resultMsg.Status = true;
                resultMsg.Data = pageData;
                resultMsg.Code = (int)EnumApiStateCode.Success;
                resultMsg.Message = "查询成功";
            }
            return resultMsg;
        }

        /// <summary>
        /// 获取微信回复消息
        /// </summary>
        /// <returns></returns>
        public string GetReplyMsgByRecive(string ReciveMsg)
        {
            string condition = @"SELECT * FROM T_ReplyMsg WHERE ReciveMsg=@ReciveMsg 
                   AND  IsDeleted<>@IsDeleted   AND  IsEnable=@IsEnable   ";//查询除当前菜单外，是否有重名且未删除的菜单
            var ReplyMsg = (this.CurrentRepository as IReplyMsgRepository).LoadByCondition(condition, new { ReciveMsg = ReciveMsg, IsDeleted = 1 , IsEnable = 1 }).FirstOrDefault();
            if (ReplyMsg != null)
            {
                return ReplyMsg.ReturnMsg;
            }
            else
            {
                return null;
            }
        }
    }
}